package com.vma.shoeSystem.controller;

import com.jfinal.core.Controller;
import com.vma.neng.tools.AppException;
import com.vma.order.service.OrderService;
import com.vma.shoeSystem.util.ReturnsUtil;
import com.vma.shoes.bizModel.model.OrderList;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenzui on 2017/4/18.
 */
public class OrderController extends Controller {
    public void qryOrderList(){
        try{
            Integer cPage = getParaToInt("cPage");
            Integer pSize = getParaToInt("pSize");
            String orderNbr = getPara("orderNbr");

            String status = getPara("status");

            String start = getPara("start");
            String end = getPara("end");
            renderJson(ReturnsUtil.returnSuccess(OrderService.qryOrderList(orderNbr,status,cPage,pSize,start,end,0)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    public void detail(){
        try{
            Map map = new HashMap();
            map = OrderService.qryOrderDetail(getPara("id"));
            setAttr("order",map);
            renderVelocity("/shop/order/detail.html");
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    public void send(){
        try {
            Integer id = getParaToInt("id");
            String wuliuType = getPara("wuliuCompany");
            String wuliuNbr = getPara("wuliuNbr");
            if(wuliuNbr == null || "".equals(wuliuNbr)){
                throw new AppException("-1","物流单号不可为空");
            }
            OrderList orderList = OrderList.dao.findById(id);
            if(!orderList.getStatus().equals("1")){
                throw new AppException("-2","未支付订单不可发货");
            }
            orderList.setWuliuType(wuliuType).setLogistics(wuliuNbr).setSendTime(new Date()).setStatus("3").update();
            renderJson(ReturnsUtil.returnSuccess("success"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
}
